package com.drinks.yiniang.ui.widget.nav;

import android.content.Context;
import android.support.annotation.IntRange;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.drinks.yiniang.R;
import com.drinks.yiniang.common.listener.OnGoodsNavigationBarClickListener;
import com.hansen.library.utils.AppUtils;
import com.hansen.library.utils.ScreenSizeUtils;

/**
 * 首页navigation bar
 * Created by han on 2018/10/17 0017.
 */

public class NavigationGoodsDetailLayout extends LinearLayout implements View.OnClickListener, TabLayout.OnTabSelectedListener {

    private Context mContext;

    private AppCompatImageView mBackImageView;
    private TabLayout mTabLayout;
    private AppCompatImageView mShareImageView;
    private AppCompatImageView mCartImageView;

    private String[] mTabArrays;

    private OnGoodsNavigationBarClickListener onNavgationBarClickListener;
    private TabLayout.OnTabSelectedListener onTabSelectedListener;
    //当前是否展示的是灰色圆形的背景
    private boolean mCurrentShowRoundBackground = false;

    public NavigationGoodsDetailLayout(Context context) {
        this(context, null);
    }

    public NavigationGoodsDetailLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NavigationGoodsDetailLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initData();
    }

    private void initData() {
        mContext = getContext();

        setOrientation(HORIZONTAL);

        int dp10 = ScreenSizeUtils.dp2px(mContext, 10);
        int dp15 = ScreenSizeUtils.dp2px(mContext, 15);
        int dp30 = ScreenSizeUtils.dp2px(mContext, 30);
        int dp32 = ScreenSizeUtils.dp2px(mContext, 32);
        //获取顶部navbar高度
        int navBarHeight = mContext.getResources().getDimensionPixelSize(R.dimen.height_navigation_bar);

        //沉浸，设置状态栏高度, 加上&& !isInEditMode() 判断是因为在EditMode模式下，会出现exception
        if (AppUtils.isVersionCodeGreaterThanOrEqualToKITKAT() && !isInEditMode()) {
            setPadding(0, (int) ScreenSizeUtils.getStatusBarHeightWithAndroid(mContext), 0, 0);
        }

        mBackImageView = new AppCompatImageView(mContext);
        mTabLayout = new TabLayout(mContext);
        mShareImageView = new AppCompatImageView(mContext);
        mCartImageView = new AppCompatImageView(mContext);

        //菜单
        mBackImageView.setId(R.id.iv_nav_bar_back);
        mBackImageView.setScaleType(ImageView.ScaleType.CENTER);
        mBackImageView.setOnClickListener(this);

        //tab
        initTab();

        //分享
        mShareImageView.setId(R.id.iv_nav_bar_share);
        mShareImageView.setImageResource(R.mipmap.icon_nav_share);
        mShareImageView.setOnClickListener(this);

        //购物车
        mCartImageView.setId(R.id.iv_nav_bar_cart);
        mCartImageView.setScaleType(ImageView.ScaleType.CENTER);
        mCartImageView.setOnClickListener(this);

        LayoutParams paramsWidth = new LayoutParams(dp32, dp32);
        LayoutParams paramsShare = new LayoutParams(LayoutParams.WRAP_CONTENT, navBarHeight);
        LayoutParams paramsTab = new LayoutParams(0, dp30);

        paramsWidth.gravity = Gravity.CENTER_VERTICAL;
        paramsWidth.leftMargin = dp10;
        paramsWidth.rightMargin = dp10;

        paramsShare.gravity = Gravity.CENTER_VERTICAL;
        paramsShare.leftMargin = dp15;

        paramsTab.leftMargin = dp30;
        paramsTab.weight = 1.0f;
        paramsTab.gravity = Gravity.BOTTOM;

        setTabLayoutAlpha(0);

        addView(mBackImageView, paramsWidth);
        addView(mTabLayout, paramsTab);
        addView(mShareImageView, paramsShare);
        addView(mCartImageView, paramsWidth);
    }

    private void initTab() {
        mTabArrays = new String[]{mContext.getString(R.string.text_goods), mContext.getString(R.string.text_evaluate),
                mContext.getString(R.string.text_detail), mContext.getString(R.string.text_recommend)};

        mTabLayout = (TabLayout) LayoutInflater.from(mContext).inflate(R.layout.layout_tab_goods_detail, null);

        for(int i = 0; i < mTabArrays.length; i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(mTabArrays[i]));
        }

        mTabLayout.addOnTabSelectedListener(this);
    }

    @Override
    public void onClick(View v) {
        if (onNavgationBarClickListener == null) {
            return;
        }

        switch (v.getId()) {
            case R.id.iv_nav_bar_back: //返回
                onNavgationBarClickListener.onBackClick(v);
                break;
            case R.id.iv_nav_bar_share://分享
                onNavgationBarClickListener.onShareClick(v);
                break;
            case R.id.iv_nav_bar_cart: //购物车
                onNavgationBarClickListener.onCartClick(v);
                break;
            default:
                break;
        }
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        if(onTabSelectedListener != null) {
            onTabSelectedListener.onTabSelected(tab);
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
        if(onTabSelectedListener != null) {
            onTabSelectedListener.onTabUnselected(tab);
        }
    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {
        if(onTabSelectedListener != null) {
            onTabSelectedListener.onTabReselected(tab);
        }
    }

    public void setTabLayoutAlpha(float alpha) {
        if(mTabLayout == null) {
            return;
        }

        alpha = alpha > 1f ? 1f : alpha < 0 ? 0 : alpha;

        int visibilityFlag = alpha <= 0.1 ? INVISIBLE : VISIBLE;

        if(alpha <= 0.1) {//当前切换到带圆形背景
            if(!mCurrentShowRoundBackground) {
                mCurrentShowRoundBackground = true;

                mBackImageView.setBackgroundDrawable(ContextCompat.getDrawable(mContext, R.drawable.shape_bkg_nav_round_back));
                mBackImageView.setImageResource(R.mipmap.icon_back_white);

                mCartImageView.setBackgroundDrawable(ContextCompat.getDrawable(mContext, R.drawable.shape_bkg_nav_round_back));
                mCartImageView.setImageResource(R.mipmap.icon_nav_cart_white);
            }
        } else {
            if(mCurrentShowRoundBackground) {
                mCurrentShowRoundBackground = false;

                mBackImageView.setBackgroundDrawable(null);
                mBackImageView.setImageResource(R.mipmap.icon_back);

                mCartImageView.setBackgroundDrawable(null);
                mCartImageView.setImageResource(R.mipmap.icon_nav_cart_black);
            }
        }

        if(visibilityFlag != mShareImageView.getVisibility()) {
            mShareImageView.setVisibility(visibilityFlag);
        }

        if(visibilityFlag != mTabLayout.getVisibility()) {
            mTabLayout.setVisibility(visibilityFlag);
        }

        mTabLayout.setAlpha(alpha);
    }

    public void setTabLayoutSelected(@IntRange(from = 0, to = 3) int position) {
        if(position > mTabLayout.getTabCount()) {
            return;
        }

        TabLayout.Tab tab = mTabLayout.getTabAt(position);

        if(tab.isSelected()) {
            return;
        }

        mTabLayout.removeOnTabSelectedListener(this);
        tab.select();
        mTabLayout.addOnTabSelectedListener(this);
    }

    public void setOnNavgationBarClickListener(OnGoodsNavigationBarClickListener onNavgationBarClickListener) {
        this.onNavgationBarClickListener = onNavgationBarClickListener;
    }

    public void setOnTabSelectedListener(TabLayout.OnTabSelectedListener onTabSelectedListener) {
        this.onTabSelectedListener = onTabSelectedListener;
    }
}
